<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>角色授权</title>
    <link href="<%=request.getContextPath()%>/views/sysdefault/css/bootstrap.min.css?v=3.3.5" rel="stylesheet">
    <link href="<%=request.getContextPath()%>/views/sysdefault/css/font-awesome.min.css?v=4.4.0" rel="stylesheet">
    <link href="<%=request.getContextPath()%>/views/sysdefault/css/plugins/jsTree/style.min.css" rel="stylesheet">
    <link href="<%=request.getContextPath()%>/views/sysdefault/css/animate.min.css" rel="stylesheet">
    <link href="<%=request.getContextPath()%>/views/sysdefault/css/style.min.css?v=4.0.0" rel="stylesheet">
    <link href="<%=request.getContextPath()%>/views/sysdefault/css/plugins/toastr/toastr.min.css" rel="stylesheet">
	
</head>

<body >


	<div class="ibox-content">

		<div id="jstree1"></div>
		
		<div class="hr-line-dashed"></div>
		<div class="form-group">
			<div class="col-sm-3 col-sm-offset-9">
				<button class="btn btn-primary" onclick="editRoleAuth();">保存内容</button>
			</div>
		</div>

	</div>

	</div>

	<script src="<%=request.getContextPath()%>/views/sysdefault/js/jquery.min.js?v=2.1.4"></script>
    <script src="<%=request.getContextPath()%>/views/sysdefault/js/bootstrap.min.js?v=3.3.5"></script>
    <script src="<%=request.getContextPath()%>/views/sysdefault/js/content.min.js?v=1.0.0"></script>
    <script src="<%=request.getContextPath()%>/views/sysdefault/js/plugins/jsTree/jstree.min.js"></script>
    <script src="<%=request.getContextPath()%>/views/sysdefault/js/plugins/toastr/toastr.min.js"></script>
    <script src="<%=request.getContextPath()%>/views/sysdefault/js/plugins/layer/layer.js"></script>
    
    <script type="text/javascript">
    
    var basePath = '<%=request.getContextPath()%>';
    var roleId = '${roleId}';
    
    toastr.options = {
  		  "closeButton": true,
  		  "debug": false,
  		  "progressBar": true,
  		  "positionClass": "toast-top-left",
  		  "showDuration": "400",
  		  "hideDuration": "1000",
  		  "timeOut": "3000",
  		  "extendedTimeOut": "1000",
  		  "showEasing": "swing",
  		  "hideEasing": "linear",
  		  "showMethod": "fadeIn",
  		  "hideMethod": "fadeOut"
  		}
    
	$(document).ready(function() {
    	
    	var funcTree = ${roleTree};
    	var reg = new RegExp("<ul></ul>","g"); //创建正则RegExp对象
    	$("#jstree1").html(getTree(funcTree,-1).replace(reg, ""));
    	
        $("#jstree1").jstree({
            "core": {
                "check_callback": true
            },
            "checkbox" : {  
                "keep_selected_style" : false,
                "three_state" : false
            },
            "plugins": ["types", "checkbox"],
            "types": {
                "default": {
                    "icon": "fa fa-file-code-o"
                },
                "root": {
                    "icon": "fa fa-folder"
                },
                "module": {
                    "icon": "fa fa-list-alt"
                },
                "menu": {
                    "icon": "fa fa-list-ul"
                },
                "link": {
                    "icon": "fa fa-chain"
                },
            }
        });
        
    });
    
    var getTree = function(data, pId){
    	var tree; 
    	tree = '<ul>'; 
        
    	for(var i in data){
    		if(data[i].pId == pId){
    			
    			tree += '<li';
    			
    			if(data[i].open){
    				tree += ' class="jstree-open"'
    			}
    			
    			if(data[i].id == 0){
    				tree += ' data-jstree={"type":"root"}';
    			}else if(data[i].funcType == 1){
    				tree += ' data-jstree={"type":"module"}';
    			}else if(data[i].funcType == 2){
    				tree += ' data-jstree={"type":"menu"}';
    			}else if(data[i].funcType == 3){
    				tree += ' data-jstree={"type":"link"}';
    			}
    			
    			tree += '>';
    			
    			tree += '<a';
    			
    			if(data[i].funcCheck){
    				tree += " class='jstree-clicked'";
    			}
    			
    			tree += " href='#' "+ 
    				 "id='" + data[i].id +
                	 "' name='" + data[i].name + 
                	 "' pId='" + data[i].pId + 
                	 "' pName='" + data[i].pName + 
                	 "' funcType='" + data[i].funcType +
                	 "' funcAcce='" + data[i].funcAcce +
                	 "' funcOrder='" + data[i].funcOrder +
                	 "' funcUrl='" + data[i].funcUrl +
                	 "' >" 
                	 + data[i].name;
                tree += "</a>";
    			tree += getTree(data, data[i].id); 
                tree += "</li>";
    		}
    	}
    	
    	return tree+"</ul>";
    }
    
    var editRoleAuth = function(){
    	
    	var ids = '';
    	var objs = $('#jstree1').jstree('get_checked', 'full');
    	
    	$.each(objs, function(i, item){
    		ids = ids + item.a_attr.id + ',';
		});
    	
    	$.ajax({
			type : "POST",
			url : basePath + "/system/editRoleFunc.ido",
			data : {"ids" : ids, "roleId" : roleId},
			dataType : "json",
			success : function(msg) {
				if (msg.errCode == '1') {
					parent.layerCallBack('2');
				}else{
					toastr.error(msg.errMsg, "提示");
				}
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				toastr.error("系统出错！", "提示");
			},
			beforeSend : function(XMLHttpRequest){
				index = layer.load(0, {
					  shade: [0.3,'#ccc'] //0.1透明度的白色背景
					});
			},
			complete : function(XMLHttpRequest, textStatus){
				layer.close(index);
			}
		});
    }
    
    </script>
    
</body>

</html>